package com.bd.scm.config;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Long类型数据转字符串
 *
 * 当后端传给前端后，Long的数据会丢失精度，在传给前端时转成字符串
 *
 */
@Configuration
public class LongConfig {


        @Bean
        public Jackson2ObjectMapperBuilderCustomizer customJackson() {
            return jacksonObjectMapperBuilder -> {
                jacksonObjectMapperBuilder.serializationInclusion(JsonInclude.Include.NON_NULL);
                // Jackson全局转换long类型为String，解决jackson序列化时long类型缺失精度问题
                // 自定义Long类型转换 超过12个数字用String格式返回，由于js的number只能表示15个数字
                jacksonObjectMapperBuilder.serializerByType(Long.class, ToStringSerializer.instance);
//            jacksonObjectMapperBuilder.serializerByType(Long.TYPE, ToStringSerializer.instance);
                jacksonObjectMapperBuilder.failOnUnknownProperties(false);

                // 驼峰字段名，不注释会有"_"
//                jacksonObjectMapperBuilder.propertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
            };
        }

}
